dnl	This file is an input file used by the GNU "autoconf" program to
dnl	generate the file "configure", which is run during Tcl installation
dnl	to configure the system for the local environment.
AC_INIT(tcl.h)
# @(#) configure.in 1.6 95/03/24 09:38:10

AC_PROG_INSTALL
AC_PROG_RANLIB
AC_PREFIX_PROGRAM(tclsh)
AC_C_CROSS
CC=${CC-cc}
AC_SUBST(CC)

#--------------------------------------------------------------------
#	Supply substitutes for missing POSIX library procedures, or
#	set flags so Tcl uses alternate procedures.
#--------------------------------------------------------------------

AC_REPLACE_FUNCS(getcwd opendir strerror strncasecmp strstr)
AC_REPLACE_FUNCS(strtol tmpnam waitpid)
AC_FUNC_CHECK(gettimeofday, , AC_DEFINE(NO_GETTOD))
AC_FUNC_CHECK(getwd, , AC_DEFINE(NO_GETWD))
AC_FUNC_CHECK(wait3, , AC_DEFINE(NO_WAIT3))

#--------------------------------------------------------------------
#	On a few very rare systems, all of the libm.a stuff is
#	already in libc.a.  Set compiler flags accordingly.
#	Also, Linux requires the "ieee" library for math to work
#	right.
#--------------------------------------------------------------------

AC_FUNC_CHECK(sin, MATH_LIBS="", MATH_LIBS="-lm")
AC_SUBST(MATH_LIBS)
AC_CHECK_LIB(ieee, main, [MATH_LIBS="$MATH_LIBS -lieee"])

#--------------------------------------------------------------------
#	Supply substitutes for missing POSIX header files.  Special
#	notes:
#	    - Sprite's dirent.h exists but is bogus.
#	    - stdlib.h doesn't define strtol, strtoul, or
#	      strtod insome versions of SunOS
#	    - some versions of string.h don't declare procedures such
#	      as strstr
#--------------------------------------------------------------------

AC_HAVE_HEADERS(unistd.h)
AC_MSG_CHECKING(dirent.h)
AC_TRY_LINK([#include <sys/types.h>
#include <dirent.h>], [
#ifndef _POSIX_SOURCE
#   ifdef __Lynx__
	/*
	 * Generate compilation error to make the test fail:  Lynx headers
	 * are only valid if really in the POSIX environment.
	 */

	missing_procedure();
#   endif
#endif
DIR *d;
struct dirent *entryPtr;
char *p;
d = opendir("foobar");
entryPtr = readdir(d);
p = entryPtr->d_name;
closedir(d);
], tcl_ok=yes, tcl_ok=no)
AC_HEADER_EGREP([Sprite version.* NOT POSIX], tcl_ok=no)
if test $tcl_ok = no; then
    AC_DEFINE(NO_DIRENT_H)
fi
AC_MSG_RESULT($tcl_ok)
AC_HEADER_CHECK(errno.h, , AC_DEFINE(NO_ERRNO_H))
AC_HEADER_CHECK(float.h, , AC_DEFINE(NO_FLOAT_H))
AC_HEADER_CHECK(limits.h, , AC_DEFINE(NO_LIMITS_H))
AC_HEADER_CHECK(stdlib.h, tcl_ok=1, tcl_ok=0)
AC_HEADER_EGREP(strtol, stdlib.h, , tcl_ok=0)
AC_HEADER_EGREP(strtoul, stdlib.h, , tcl_ok=0)
AC_HEADER_EGREP(strtod, stdlib.h, , tcl_ok=0)
if test $tcl_ok = 0; then
    AC_DEFINE(NO_STDLIB_H)
fi
AC_HEADER_CHECK(string.h, tcl_ok=1, tcl_ok=0)
AC_HEADER_EGREP(strstr, string.h, , tcl_ok=0)
AC_HEADER_EGREP(strerror, string.h, , tcl_ok=0)
if test $tcl_ok = 0; then
    AC_DEFINE(NO_STRING_H)
fi
AC_HEADER_CHECK(sys/time.h, , AC_DEFINE(NO_SYS_TIME_H))
AC_HEADER_CHECK(sys/wait.h, , AC_DEFINE(NO_SYS_WAIT_H))

#--------------------------------------------------------------------
#	On some systems strstr is broken: it returns a pointer even
#	even if the original string is empty.
#--------------------------------------------------------------------

AC_MSG_CHECKING([proper strstr implementation])
AC_TEST_PROGRAM([
extern int strstr();
int main()
{
    exit(strstr("\0test", "test") ? 1 : 0);
}
], tck_ok=yes, tcl_ok=no)
if test $tcl_ok = yes; then
    AC_MSG_RESULT(yes)
else
    AC_MSG_RESULT([broken, using substitute])
    LIBOBJS="$LIBOBJS strstr.o"
fi

#--------------------------------------------------------------------
#	Check for strtoul function.  This is tricky because under some
#	versions of AIX strtoul returns an incorrect terminator
#	pointer for the string "0".
#--------------------------------------------------------------------

AC_FUNC_CHECK(strtoul, tcl_ok=1, tcl_ok=0)
AC_TEST_PROGRAM([
extern int strtoul();
int main()
{
    char *string = "0";
    char *term;
    int value;
    value = strtoul(string, &term, 0);
    if ((value != 0) || (term != (string+1))) {
        exit(1);
    }
    exit(0);
}], , tcl_ok=0)
if test $tcl_ok = 0; then
    test -n "$verbose" && echo "	Adding strtoul.o."
    LIBOBJS="$LIBOBJS strtoul.o"
fi

#--------------------------------------------------------------------
#	Check for the strtod function.  This is tricky because in some
#	versions of Linux strtod mis-parses strings starting with "+".
#--------------------------------------------------------------------

AC_FUNC_CHECK(strtod, tcl_ok=1, tcl_ok=0)
AC_TEST_PROGRAM([
extern double strtod();
int main()
{
    char *string = " +69";
    char *term;
    double value;
    value = strtod(string, &term);
    if ((value != 69) || (term != (string+4))) {
	exit(1);
    }
    exit(0);
}], , tcl_ok=0)
if test $tcl_ok = 0; then
    test -n "$verbose" && echo "	Adding strtod.o."
    LIBOBJS="$LIBOBJS strtod.o"
fi

#--------------------------------------------------------------------
#	Check for various typedefs and provide substitutes if
#	they don't exist.
#--------------------------------------------------------------------

AC_MODE_T
AC_PID_T
AC_SIZE_T
AC_UID_T

#--------------------------------------------------------------------
#	If a system doesn't have an opendir function (man, that's old!)
#	then we have to supply a different version of dirent.h which
#	is compatible with the substitute version of opendir that's
#	provided.  This version only works with V7-style directories.
#--------------------------------------------------------------------

AC_FUNC_CHECK(opendir, , AC_DEFINE(USE_DIRENT2_H))

#--------------------------------------------------------------------
#	Check for the existence of sys_errlist (this is only needed if
#	there's no strerror, but I don't know how to conditionalize the
#	check).
#--------------------------------------------------------------------

AC_MSG_CHECKING(sys_errlist)
AC_TRY_LINK(, [
extern char *sys_errlist[];
extern int sys_nerr;
sys_errlist[sys_nerr-1][0] = 0;
], tcl_ok=yes, tcl_ok=no)
AC_MSG_RESULT($tcl_ok)
if test $tcl_ok = no; then
    AC_DEFINE(NO_SYS_ERRLIST)
fi

#--------------------------------------------------------------------
#	The check below checks whether <sys/wait.h> defines the type
#	"union wait" correctly.  It's needed because of weirdness in
#	HP-UX where "union wait" is defined in both the BSD and SYS-V
#	environments.  Checking the usability of WIFEXITED seems to do
#	the trick.
#--------------------------------------------------------------------

AC_MSG_CHECKING([union wait])
AC_TRY_LINK([#include <sys/types.h> 
#include <sys/wait.h>], [
union wait x;
WIFEXITED(x);		/* Generates compiler error if WIFEXITED
			 * uses an int. */
], tcl_ok=yes, tcl_ok=no)
AC_MSG_RESULT($tcl_ok)
if test $tcl_ok = no; then
    AC_DEFINE(NO_UNION_WAIT)
fi

#--------------------------------------------------------------------
#	Check to see whether the system supports the matherr function
#	and its associated type "struct exception".
#--------------------------------------------------------------------

AC_MSG_CHECKING([matherr support])
AC_TRY_COMPILE([#include <math.h>], [
struct exception x;
x.type = DOMAIN;
x.type = SING;
], tcl_ok=yes, tcl_ok=no)
AC_MSG_RESULT($tcl_ok)
if test $tcl_ok = yes; then
    AC_DEFINE(NEED_MATHERR)
fi

#--------------------------------------------------------------------
#	Check to see whether the system provides a vfork kernel call.
#	If not, then use fork instead.
#--------------------------------------------------------------------

AC_FUNC_CHECK(vfork, , AC_DEFINE(vfork, fork))

AC_OUTPUT(Makefile)
